package com.sunshine.controller;

import com.sunshine.Service.impl.UserServiceImpl;
import com.sunshine.domain.UserDTO;
import com.sunshine.domain.user;
import com.sunshine.domain.userPhone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.stream.events.Comment;

@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserServiceImpl userService;

    @GetMapping("/hello")
    public String hello() {
        return "测试成功了，离找到实习又近一步啦";
    }

    @PostMapping("/register")
    public user register(String username, String password)
    {
        return userService.register(username, password);
    }


    @PostMapping("/login")
    public UserDTO login(String username, String password)
    {

        user login = userService.login(username, password);
        if (login != null)
        {
            UserDTO userDTO = new UserDTO();
            userDTO.setId(login.getId());
            userDTO.setUsername(login.getUsername());
            userDTO.setStatus(login.getStatus());
            userDTO.setToken(login.getToken());
            return userDTO;
        }
        return null;
    }


    @PostMapping("/loginByPhone")
    public UserDTO loginByPhone(userPhone phone)
    {
        userService.loginByPhone(phone );
        return loginByPhone(phone);
    }

    /**
     * 用户注销
     */
    @DeleteMapping("/logout/{id}")
    public String logout(@PathVariable Integer id)
    {
        String logout = userService.logout(id);
        if (logout != null)
        {
            return "注销成功";
        }else
            return "注销失败";

    }

    /**
     * 修改用户信息
     */
    @PutMapping("/updateUserInfo/{id}")
    public String updateUserInfo(@PathVariable Integer id, String username, String phone, MultipartFile head)
        {
        user updateUserInfo = userService.updateUserInfo(id, username, phone, head);
/*        if (updateUserInfo != null)
        {
            return  updateUserInfo +"修改成功";
        }else
            return "修改失败";*/
            return updateUserInfo +"修改成功";
    }
    /**
     * 修改密码
     */
    @PutMapping("/updatePassword")
    public String updatePassword( Integer id, String oldpassword,String newpassword)
    {
        String updatePassword = userService.updatePassword(id, oldpassword,newpassword);
        return updatePassword;
    }


}